﻿package njClass.net{
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	public class DataLoader{
		private var CallBackFunction:Function;
		private var bit:String;
		private var url:URLRequest;
		private var methodKind:String;
		
		private var variables:URLVariables=new URLVariables();
		private var loader:URLLoader=new URLLoader;
		
		private var _result:String;
		
		private var BytesTotal:Number=0;
		private var BytesLoaded:Number=0;
		
		private var isLoading:Boolean=false;
		
		//加载url
		//methodKind=["POST","GET"]
		//CallBackFunction 返回函数，可以为空
		//startLink 为true则自动加载，为false则需要手动加载
		public function DataLoader(url:String,CallBackFunction:Function,methodKind:String="POST",startLink:Boolean=true,bit:String="utf-8",DataFormat:String="BINARY") {
			this.CallBackFunction=CallBackFunction;
			this.bit=bit;
			this.url=new URLRequest(url);
			this.methodKind=(methodKind=="POST")?"POST":"GET";
			
			if(DataFormat=="BINARY"){
				loader.dataFormat = URLLoaderDataFormat.BINARY;
				loader.addEventListener(Event.COMPLETE,loaded_BINARY);
			}else if(DataFormat=="VARIABLES"){
				loader.dataFormat = URLLoaderDataFormat.VARIABLES;
				loader.addEventListener(Event.COMPLETE,loaded_TEXT);
			}else if(DataFormat=="TEXT"){
				loader.dataFormat = URLLoaderDataFormat.TEXT;
				loader.addEventListener(Event.COMPLETE,loaded_TEXT);
				loader.addEventListener(ProgressEvent.PROGRESS,loading_TEXT);
			}
			
			this.url.method=(methodKind=="POST")?URLRequestMethod.POST:URLRequestMethod.GET;
			
			if(startLink){
				start();
			}
		}
		
		//手动加载
		public function start():void{
			var num:int=Math.random()*10000;
			upData({"name":"RandomNum","value":num});
			
			url.data = variables;
			isLoading=true;
			loader.load(url);
		}
		
		//加载进度处理
		private function loading_TEXT(evt:ProgressEvent):void{
			//trace(evt.bytesLoaded,evt.bytesTotal);
			this.BytesLoaded=evt.bytesLoaded;
			this.BytesTotal=evt.bytesTotal;
		}
		
		private function loaded_BINARY(evt:Event):void {
			isLoading=false;
			var BA:ByteArray = ByteArray(loader.data);
			//CallBackFunction(BA);
			//trace(BA.length);
			var strJson:String = BA.readMultiByte(BA.length, this.bit);
			_result=strJson;
			
			if(CallBackFunction!=null){
				CallBackFunction(_result);
			}
		}
		
		private function loaded_TEXT(evt:Event):void {
			isLoading=false;
			_result=loader.data;
			
			if(CallBackFunction!=null){
				CallBackFunction(_result);
			}
		}
		
		//添加参数值 格式：{"name":参数名,"value":数值}
		public function upData(json:Object):void {
			variables.decode(json.name + "=" + json.value);
		}
		
		//返回获得的字段
		public function get result():String{
			return _result;
		}
		
		//返回加载进度项
		public function get ProgressData():Object{
			return {"bytesLoaded":this.BytesLoaded,"bytesTotal":this.BytesTotal};
		}
		
		public function DisPose():void{
			if(isLoading==false){
				trace("没有在加载");
				return;
			}
			loader.close();
			isLoading=false;
		}
	}
}